'''
Created on 09/10/2012

@author: rcasal
'''
from gluon.sqlhtml import SQLFORM
from domain.dal2 import DAL2
from gluon.html import TABLE, FIELDSET, DIV, TD, TR, LEGEND

    
class FormFactory(object):
    '''
    classdocs
    '''
    
    @staticmethod
    def carga_comunicacion_consulta(table_comunicacion, table_consulta):
        form =  SQLFORMCOMUNACION.factory(table_comunicacion, table_consulta, _id = "com_telefonica_afiliado", _name = "com_telefonica_afiliado", submit_button='Enviar')
        return form

    @staticmethod
    def carga_comunicacion_mail(table_comunicacion, table_consulta, table_mail_consulta, table_mail_respuesta, submit_button='Enviar'):
        form = SQLFORMCOMUNICACIONMAIL.factory(table_comunicacion, table_consulta, table_mail_consulta, table_mail_respuesta)
        return form
    @staticmethod
    def carga_consulta(table_consulta):
        form = SQLFORMCONSULTA.factory(table_consulta, submit_button='Enviar')
        return form
    
    @staticmethod
    def resolucion_pendiente(table_pendiente):
        form = SQLFORMPENDIENTE.factory(table_pendiente, submit_button='Enviar')
        return form
    
    @staticmethod
    def alta_usuarios(tabla_usuario):
        form = SQLFORM.factory(tabla_usuario, submit_button='Enviar')
        return form
    
class SQLFORMPENDIENTE(SQLFORM):
    
    """
        factory: modificado para crear una tabla con fieldsets, 
        debe modificarse de acuerdo se vayan agregando o eliminando
        campos al formulario, sino arroja una excepcion
        tambien se agrego el tag DIV(id = "id_" + id) para solucionar las recargas
        de las combos con ajax en internet explorer 
    """

    
    @staticmethod
    def factory(*fields, **attributes):
        """
        generates a SQLFORM for the given fields.
        Internally will build a non-database based data model
        to hold the fields.
        """
        # Define a table name, this way it can be logical to our CSS.
        # And if you switch from using SQLFORM to SQLFORM.factory
        # your same css definitions will still apply.

        table_name = attributes.get('table_name', 'no_table')

        # So it won't interfear with SQLDB.define_table
        if 'table_name' in attributes:
            del attributes['table_name']

        return SQLFORMPENDIENTE(DAL2(None).define_table(table_name, *fields),
                       **attributes)
    
    def createform(self, xfields):
        if self.formstyle == 'table3cols':
            i = 0
            tableint = TABLE()
            fieldsets = []
            for id,a,b,c in xfields:
                i = i + 1
                td_b = self.field_parent[id] = TD(DIV(b, _id = "div_"+id),_class='w2p_fw')
                tableint.append(TR(TD(a,_class='w2p_fl'),
                                    td_b,
                                    TD(c,_class='w2p_fc'),_id=id, _name=id))
                
                if (i==3):
                    fieldset = FIELDSET(
                        LEGEND("Resolucion"),
                    )
                    fieldset.append(tableint)
                    fieldsets.append(fieldset)
                    tableint = TABLE()
                elif (i>3):
                    fieldset = FIELDSET(
                        LEGEND("Guardar"),
                    )
                    fieldset.append(tableint)
                    fieldsets.append(fieldset)
                    tableint = TABLE()
                    
            table = TABLE()
            for fieldset in fieldsets:
                table.append(fieldset)
             
        else:
            raise RuntimeError, 'formstyle not supported'
        return table   

class SQLFORMCOMUNACION(SQLFORM):
    
    """
            factory: modificado para crear una tabla con fieldsets, 
            debe modificarse de acuerdo se vayan agregando o eliminando
            campos al formulario, sino arroja una excepcion
            tambien se agrego el tag DIV(id = "id_" + id) para solucionar las recargas
            de las combos con ajax en internet explorer 
    """
        
    @staticmethod
    def factory(*fields, **attributes):
        """
        generates a SQLFORM for the given fields.
    
        Internally will build a non-database based data model
        to hold the fields.
        """
        # Define a table name, this way it can be logical to our CSS.
        # And if you switch from using SQLFORM to SQLFORM.factory
        # your same css definitions will still apply.
    
        table_name = attributes.get('table_name', 'no_table')
    
        # So it won't interfear with SQLDB.define_table
        if 'table_name' in attributes:
            del attributes['table_name']
    
        return SQLFORMCOMUNACION(DAL2(None).define_table(table_name, *fields),**attributes)
        
    def createform(self, xfields):
        if self.formstyle == 'table3cols':
            table = TABLE()
            i = 0
            fieldsets = []
            tableint = TABLE()
            for id,a,b,c in xfields:
                i = i + 1
                td_b = self.field_parent[id] = TD(DIV(b, _id = "div_"+id),_class='w2p_fw')
                tableint.append(TR(TD(a,_class='w2p_fl'),
                                    td_b,
                                    TD(c,_class='w2p_fc'),_id=id, _name=id))
                
                if i == 12:
                    fieldset = FIELDSET(
                        LEGEND("Datos del consultante"),
                    )
                    fieldset.append(tableint)
                    fieldsets.append(fieldset)
                    tableint = TABLE()
                    
                if i == 17:
                    fieldset = FIELDSET(
                        LEGEND("Datos de la consulta"),
                    )
                    fieldset.append(tableint)
                    fieldsets.append(fieldset)
                    tableint = TABLE()
                    
                if i > 17:
                    fieldset = FIELDSET(
                        LEGEND("Guardar"),
                    )
                    fieldset.append(tableint)
                    fieldsets.append(fieldset)
                    tableint = TABLE()

                        
            table = TABLE()
            for fieldset in fieldsets:
                table.append(fieldset)
                 
        else:
            raise RuntimeError, 'formstyle not supported'
        return table


class SQLFORMCONSULTA(SQLFORM):
    
    """
        factory: modificado para crear una tabla con fieldsets, 
        debe modificarse de acuerdo se vayan agregando o eliminando
        campos al formulario, sino arroja una excepcion
        tambien se agrego el tag DIV(id = "id_" + id) para solucionar las recargas
        de las combos con ajax en internet explorer 
    """
    
    @staticmethod
    def factory(*fields, **attributes):
        """
        generates a SQLFORM for the given fields.
        Internally will build a non-database based data model
        to hold the fields.
        """
        # Define a table name, this way it can be logical to our CSS.
        # And if you switch from using SQLFORM to SQLFORM.factory
        # your same css definitions will still apply.

        table_name = attributes.get('table_name', 'no_table')

        # So it won't interfear with SQLDB.define_table
        if 'table_name' in attributes:
            del attributes['table_name']

        return SQLFORMCONSULTA(DAL2(None).define_table(table_name, *fields),
                       **attributes)
    
    def createform(self, xfields):
        if self.formstyle == 'table3cols':
            table = TABLE()
            i = 0
            fieldset1 = FIELDSET(
                        LEGEND("Datos de la consulta")
                    )
            fieldset2 = FIELDSET(
                    )
            tableint = TABLE()
            for id,a,b,c in xfields:
                i = i + 1
                td_b = self.field_parent[id] = TD(DIV(b, _id = "div_"+id),_class='w2p_fw')
                tableint.append(TR(TD(a,_class='w2p_fl'),
                                    td_b,
                                    TD(c,_class='w2p_fc'),_id=id, _name=id))
                
                if (i==5):
                    
                    fieldset1.append(tableint)
                    tableint = TABLE()
                elif (i>5):
                    
                    fieldset2.append(tableint)
                    tableint = TABLE()
                    
            table = TABLE()
            table.append(fieldset1)
            table.append(fieldset2)
             
        else:
            raise RuntimeError, 'formstyle not supported'
        return table        
    
class SQLFORMCOMUNICACIONMAIL(SQLFORM):
    
    """
            factory: modificado para crear una tabla con fieldsets, 
            debe modificarse de acuerdo se vayan agregando o eliminando
            campos al formulario, sino arroja una excepcion
            tambien se agrego el tag DIV(id = "id_" + id) para solucionar las recargas
            de las combos con ajax en internet explorer 
    """
        
    @staticmethod
    def factory(*fields, **attributes):
        """
        generates a SQLFORM for the given fields.
    
        Internally will build a non-database based data model
        to hold the fields.
        """
        # Define a table name, this way it can be logical to our CSS.
        # And if you switch from using SQLFORM to SQLFORM.factory
        # your same css definitions will still apply.
    
        table_name = attributes.get('table_name', 'no_table')
    
        # So it won't interfear with SQLDB.define_table
        if 'table_name' in attributes:
            del attributes['table_name']
    
        return SQLFORMCOMUNACION(DAL2(None).define_table(table_name, *fields),**attributes)
        
    def createform(self, xfields):
        if self.formstyle == 'table3cols':
            table = TABLE()
            i = 0
                
            tableint = TABLE()
            for id,a,b,c in xfields:
                i = i + 1
                td_b = self.field_parent[id] = TD(DIV(b, _id = "div_"+id),_class='w2p_fw')
                tableint.append(TR(TD(a,_class='w2p_fl'),
                                    td_b,
                                    TD(c,_class='w2p_fc'),_id=id, _name=id))
                    
                if (i==15):
                    fieldset1 = FIELDSET(
                        LEGEND("Datos del consultante"),
                    )
                    fieldset1.append(tableint)
                    tableint = TABLE()
                elif (i==20):
                    fieldset2 = FIELDSET(
                        LEGEND("Datos de la consulta")
                    )
                    fieldset2.append(tableint)
                    tableint = TABLE()
                elif (i>20):
                    fieldset3 = FIELDSET(
                    )
                    fieldset3.append(tableint)
                    tableint = TABLE()
                        
            table = TABLE()
            table.append(fieldset1)
            table.append(fieldset2)
            table.append(fieldset3)
                 
        else:
            raise RuntimeError, 'formstyle not supported'
        return table
